//
//  ChatVC.m
//  HXDemo
//
//  Created by 李亚军 on 16/11/21.
//  Copyright © 2016年 李亚军. All rights reserved.
//

#import "ChatVC.h"
#import "ChatBarVC.h"
#import "BoardTextView.h"

@interface ChatVC ()<UITableViewDelegate , UITableViewDataSource>

#pragma mark - chatBar

/// 聊天工具bar
@property(nonatomic , strong) ChatBarVC *chatBarVC;


/// 聊天工具bar 的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatBarHeight;

/// 聊天工具bar 距离屏幕底部的距离
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatBarMarginToBottom;

/// 键盘是否弹出,默认no
@property(nonatomic , assign) BOOL keyboardIsApperance;

//@property (weak, nonatomic) IBOutlet ChatTableView *tableView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/// 记录键盘消失状态:YES 点击表情,工具按钮隐藏键盘 ; NO 点击cell 或者拖拽隐藏键盘
@property(nonatomic , assign) BOOL isClickToolBarActionHiddenKeyboard;

/// 是否开始拖动
@property(nonatomic , assign) BOOL isBeginDrag;

@end

@implementation ChatVC

- (void)loadView {
    [super loadView];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initDefaultAttributes];
}

- (void)viewDidLayoutSubviews {
    
    [super viewDidLayoutSubviews];
    
    [self initDefaultAttributes];
}

/// 初始化默认属性
- (void)initDefaultAttributes {
    [self addObserveOfKeyBoard];
    
    self.keyboardIsApperance = NO;
    
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:9 inSection:0] atScrollPosition:(UITableViewScrollPositionBottom) animated:YES];
    
    [self chatBarClickView];
}


/// 添加观察者模式
- (void)addObserveOfKeyBoard {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    }
    
    return cell;
}

#pragma mark - scrollViewDelegate 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    self.isClickToolBarActionHiddenKeyboard = NO; // 拖拽隐藏键盘,屏蔽滚动到底部的动画
    [self.chatBarVC.textView resignFirstResponder];
}



#pragma mark - UITableViewDatasource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /// 点击 cell 隐藏键盘,需要触发隐藏键盘方法中的动画效果,所以这里设置NO
    self.isClickToolBarActionHiddenKeyboard = NO;
    [self.chatBarVC.textView resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}



#pragma mark - action
/// 返回
- (IBAction)backAction:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

/// 键盘弹出
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
    
    //取得键盘最后的frame(根据userInfo的key----UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";)
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.size.height - 180;
    
    /// 跟随键盘弹出高度处理
    [UIView animateWithDuration:0.5 animations:^{

        self.chatBarMarginToBottom.constant = transformY;
        self.chatBarVC.toolMarginToBottom.constant = -180 - transformY;
        self.chatBarVC.emotionMarginToBottom.constant = -180 - transformY;
        [self.view layoutIfNeeded];
        [self.chatBarVC.toolButton setBackgroundColor:[UIColor clearColor]];
        [self.chatBarVC.emotionButton setBackgroundColor:[UIColor clearColor]];
        self.keyboardIsApperance = YES;
    }];

    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:9 inSection:0] atScrollPosition:(UITableViewScrollPositionBottom) animated:YES];
}

/// 键盘隐藏
- (void)keyboardWillHideNotification:(NSNotification *)notification {
    
    if (self.isClickToolBarActionHiddenKeyboard) {
        return; // 点击表情,工具按钮会隐藏键盘,此时需要拦截下面的操作
    }
    
    self.chatBarMarginToBottom.constant = -180;
    self.chatBarHeight.constant = 230;
    self.chatBarVC.toolMarginToBottom.constant = 0;
    self.chatBarVC.emotionMarginToBottom.constant = 0;
    [self.view layoutIfNeeded];
    self.keyboardIsApperance = NO;
}



#pragma mark - 工具栏action
/// 聊天工具控制器点击事件
- (void)chatBarClickView {
    
    __weak typeof(self) weakSelf = self;
    
    /// 显示聊天控制器 工具栏
    self.chatBarVC.showToolBarBlock = ^() {
        
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [UIView animateWithDuration:0.5 animations:^{
            
            strongSelf.chatBarMarginToBottom.constant = 0;
            [strongSelf.view layoutIfNeeded];
        }];
    };
    
    /// 隐藏聊天控制器 工具栏
    self.chatBarVC.hiddenToolBarBlock = ^() {
        
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [UIView animateWithDuration:0.5 animations:^{
            
            strongSelf.chatBarMarginToBottom.constant = -180;
            [strongSelf.view layoutIfNeeded];
        }];
    };
    
    /// 聊天控制器完全弹出后,隐藏键盘
    self.chatBarVC.hiddenKeyboardBlock = ^(NSString *clickType) {
        
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.isClickToolBarActionHiddenKeyboard = YES;
        [strongSelf.chatBarVC.textView resignFirstResponder];
        [UIView animateWithDuration:0.3 animations:^{
            
            if ([clickType isEqualToString:@"0"]) {
                
                strongSelf.chatBarMarginToBottom.constant = -180;
            }else {
                strongSelf.chatBarMarginToBottom.constant = 0;
            }
            
            strongSelf.chatBarHeight.constant = 230;
            [strongSelf.view layoutIfNeeded];
        }];
    };
    
    self.chatBarVC.textViewChangeBlock = ^(CGFloat heightOfFontToIncrease) {
        
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.chatBarHeight.constant = 230 + heightOfFontToIncrease;
    };
    
}


#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"ChatBarVCID"]) {
        self.chatBarVC = segue.destinationViewController;
    }
}

- (void)dealloc {
    
    [self.chatBarVC.view removeFromSuperview];
    self.chatBarVC.view = nil;
    [self.chatBarVC removeFromParentViewController];
    self.chatBarVC = nil;
    NSLog(@"聊天页面销毁了");
}

@end
